package com.qifn.blog.Controller.Admin;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Model.*;
import com.qifn.blog.Service.BlogService;
import com.qifn.blog.Service.TagService;
import com.qifn.blog.Service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminIndexController {

    @Resource
    private BlogService blogService;

    @Resource
    private TypeService typeService;

    @Resource
    private TagService tagService;

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        List<Type> typeList = typeService.queryAdminIndexAllType();
        PageInfo<Blog> blogPageInfo = blogService.adminIndexQueryBlog(pageNum,2);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.addObject("typeList",typeList);
        mv.setViewName("admin/index");
        return mv;
    }

    @GetMapping("/search")
    public ModelAndView search(@RequestParam(defaultValue = "1") Integer pageNum,String title,String typeId,String recommend){
        ModelAndView mv = new ModelAndView();
        List<Type> typeList = typeService.queryAdminIndexAllType();
        PageInfo<Blog> blogPageInfo = blogService.adminIndexSearchBlog(title,typeId,recommend,pageNum,2);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.addObject("typeList",typeList);
        mv.setViewName("admin/index");
        return mv;
    }

    @GetMapping("/release")
    public ModelAndView release(){
        ModelAndView mv = new ModelAndView();
        List<Tag> tagList = tagService.queryAdminReleaseAllTag();
        List<Type> typeList = typeService.queryAdminIndexAllType();
        mv.addObject("tagList",tagList);
        mv.addObject("typeList",typeList);
        mv.setViewName("admin/blogrelease");
        return mv;
    }

    @PostMapping("/blogRelease")
    public ModelAndView blogRelease(Blog blog, Integer typeId, String tagId){
        ModelAndView mv = new ModelAndView();
        Type type = new Type();
        type.setId(typeId);
        blog.setType(type);
        List<Tag> tagList = new ArrayList<>();
        for(int i = 0;i < tagId.length();i += 2){
            Tag tag = new Tag();
            Integer id = Integer.valueOf(tagId.substring(i,i + 1));
            tag.setId(id);
            tagList.add(tag);
        }
        if(blog.getRecommend() == null){
            blog.setRecommend("0");
        }else {
            blog.setRecommend("1");
        }
        blog.setTags(tagList);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        blog.setUpdateTime(simpleDateFormat.format(date));
        User user = new User();
        user.setId(1);
        blog.setUser(user);
        boolean canRelease = blogService.adminReleaseBlog(blog,blog.getTags());
        if(canRelease){
            mv.addObject("success","blog发布成功");
        }else {
            mv.addObject("fail","blog发布失败");
        }
        mv.setViewName("redirect:/admin/release");
        return mv;
    }

    @PutMapping("/blogUpdate")
    public ModelAndView blogUpdate(Integer blogId){
        ModelAndView mv = new ModelAndView();
        Blog blog = blogService.adminUpdateBlog(blogId);
        List<Type> typeList = typeService.queryAdminIndexAllType();
        List<Tag> tagList = tagService.queryAdminReleaseAllTag();
        List<Tag> blogTagList = blog.getTags();
        String tagId = String.valueOf(blogTagList.get(0).getId());
        for(int i = 1;i < blogTagList.size();i++){
            tagId = tagId + "," + blogTagList.get(i).getId();
        }
        if(blog.getRecommend().equals("1")){
            blog.setRecommend("on");
        }else {
            blog.setRecommend("off");
        }
        mv.addObject("tagId",tagId);
        mv.addObject("blog",blog);
        mv.addObject("typeList",typeList);
        mv.addObject("tagList",tagList);
        mv.setViewName("admin/blogupdate");
        return mv;
    }

    @PutMapping("/blogUpdateRelease")
    public ModelAndView blogUpdateRelease(String blogId ,Blog blog, Integer typeId, String tagId) {
        ModelAndView mv = new ModelAndView();
        if(blog.getRecommend() != null){
            blog.setRecommend("1");
        }else {
            blog.setRecommend("0");
        }
        blog.setId(Integer.valueOf(blogId));
        List<Tag> tagList = new ArrayList<>();
        for(int i = 0;i < tagId.length();i += 2){
            Tag tag = new Tag();
            tag.setId(Integer.valueOf(tagId.substring(i,i+1)));
            tagList.add(tag);
        }
        blog.setTags(tagList);
        Type type = new Type();
        type.setId(typeId);
        blog.setType(type);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        blog.setUpdateTime(simpleDateFormat.format(date));
        boolean updated = blogService.adminReleaseUpdateBlog(blog);
        if(updated){
            mv.addObject("message","博客更新成功");
        }else {
            mv.addObject("message","博客更新失败");
        }
        mv.setViewName("redirect:/admin/index");
        return mv;
    }

    @DeleteMapping("/deleteBlog")
    public ModelAndView deleteBlog(Integer blogId){
        ModelAndView mv = new ModelAndView();
        boolean flag = blogService.adminIndexDeleteBlog(blogId);
        if(flag){
            mv.addObject("message","删除成功");
        }else {
            mv.addObject("message","删除失败");
        }
        mv.setViewName("redirect:/admin/index");
        return mv;
    }
}
